package io.renren.modules.humanResources.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.humanResources.dao.ProductDao;
import io.renren.modules.humanResources.dto.*;
import io.renren.modules.humanResources.entity.ProductEntity;
import io.renren.modules.humanResources.service.CategoryService;
import io.renren.modules.humanResources.service.InboundService;
import io.renren.modules.humanResources.service.InventoryService;
import io.renren.modules.humanResources.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产品信息表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-03-28
 */
@Service
public class ProductServiceImpl extends CrudServiceImpl<ProductDao, ProductEntity, ProductDTO> implements ProductService {

    @Lazy
    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public QueryWrapper<ProductEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String title = (String)params.get("title");

        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(title), "title", title);

        return wrapper;
    }


    @Override
    public PageData<ProductDTO> productPage(Map<String, Object> params) {
        // 获取所有的分类
        Map<String, Object> map = new HashMap<>();
        List<CategoryDTO> categoryDtoList = categoryService.list(map);
        PageData<ProductDTO> page = this.page(params);
        page.getList().forEach(e -> {
            CategoryDTO categoryDTO = categoryDtoList.stream().filter(j -> j.getId().compareTo(e.getCategoryId()) == 0).findAny().orElse(null);
            e.setCategoryTitle(categoryDTO == null ? "" : categoryDTO.getTitle());
        });
        return page;
    }

    @Override
    public List<ProductDTO> getByProTitle(String productTitle) {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(productTitle), "title", productTitle);
        List<ProductEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, ProductDTO.class);
    }

    @Override
    public List<MenuProductDTO> productList(Map<String, Object> params) {
        List<ProductEntity> entityList = this.baseDao.selectList(getWrapper(params));
        List<ProductDTO> dtoList = ConvertUtils.sourceToTarget(entityList, ProductDTO.class);
        // 获取所有的库存
        Map<String, Object> map2 = new HashMap<>();
        List<InventoryDTO> inventoryDtoList = inventoryService.list(map2);
        if (CollectionUtils.isNotEmpty(inventoryDtoList)) {
            dtoList.forEach(e -> {
                InventoryDTO inventoryDTO = inventoryDtoList.stream().filter(j -> j.getProductId().compareTo(e.getId()) == 0).findAny().orElse(null);
                e.setQuantity(inventoryDTO == null ? 0 : inventoryDTO.getQuantity());
            });
        }
        // 获取所有的分类
        Map<String, Object> map1 = new HashMap<>();
        List<CategoryDTO> categoryDtoList = categoryService.list(map1);

        List<MenuProductDTO> list = new ArrayList<>();
        // 分类分组
        Map<Long, List<ProductDTO>> categoryMap = dtoList.stream().collect(Collectors.groupingBy(ProductDTO::getCategoryId));
        categoryMap.keySet().forEach(key -> {
            CategoryDTO categoryDTO = categoryDtoList.stream().filter(j -> j.getId().compareTo(key) == 0).findAny().orElse(null);
            MenuProductDTO menuProductDTO = new MenuProductDTO();
            menuProductDTO.setCategoryId(key);
            menuProductDTO.setCategoryTitle(categoryDTO == null ? "" : categoryDTO.getTitle());
            menuProductDTO.setProductDtoList(categoryMap.get(key));
            list.add(menuProductDTO);
        });
        return list;
    }

}